$(function () {
    layui.config({
        base: './../../../static/lib/dist/'
    });

    layui.use([ 'laydate', 'form', 'notice', 'table', 'selectPlus'],function () {
        var form = layui.form;
        var notice = layui.notice;
        var table = layui.table;
        var laydate = layui.laydate;
        var selectPlus = layui.selectPlus;

        function inittable() {
            let initjson = {
                elem: '#prolist',
                url: 'http://127.0.0.1:8098/amc/sys/base/menu/pro/page',
                headers: {'authorization': layui.data('user').token},
                id: 'pro_tab',
                cols: [[
                    {field:'id', title: 'ID', sort: true}
                    ,{field:'proName', edit: 'text',title: '项目名称'}
                    ,{field:'proMark', title: '项目标示'}
                    ,{fixed: 'right', title:'操作', toolbar: '#row_bar', width:150}
                ]]
            }
            tableinit(table, initjson)
        }
        inittable()

        function tableReload(){
            table.reload('pro_tab', {
                where: {

                } //设定异步数据接口的额外参数
            });
        }

        form.on('submit(save)', function (data) {
            savePro(data.field).then(value => {
                if(value.code == 200){
                    layer.msg('保存成功', {icon: 6});
                    tableReload()
                }else{
                    notice.error('保存失败，'+value.msg)
                }
            }).catch(reason => {
                notice.error('保存失败，服务器未连接')
            })
        })

        //监听行工具事件
        table.on('tool(profilter)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确认要删除吗', function(index){
                    deletePro(data.id).then(value => {
                        if(value.code == 200){
                            layer.msg('删除成功', {icon: 1});
                            tableReload()
                        }else{
                            layer.msg('删除失败，'+value.msg)
                        }
                    }).catch(reason => {
                        layer.msg('删除失败，服务器未连接')
                    })
                });
            }
        });

        //监听单元格编辑
        table.on('edit(profilter)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            data[field] = value
            updatePro(data).then(value => {
                if(value.code == 200){
                    layer.msg('修改成功', {icon: 1});
                    tableReload()
                }else{
                    layer.msg('修改失败，'+value.msg)
                }
            }).catch(reason => {
                layer.msg('服务器未连结')
            })
        });
    })
})